package com.ant.androiddemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.ant.androiddemo.constant.Global;
import com.ant.androiddemo.util.ToastUtil;

public class RegActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = Global.LOG_TAG;
    private static final String SPF_TAG = Global.SPF_TAG;
    //回跳登录页面状态
    public static int RESULT_CODE_REGISTER = 0;
    private Button btnRegister;
    private EditText etAccount, etPassword, etPasswordConfirm;
    private CheckBox cbAgree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);
        etPasswordConfirm = findViewById(R.id.et_password_confirm);
        cbAgree = findViewById(R.id.cb_agree);
        btnRegister = findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        String account = etAccount.getText().toString();
        String password = etPassword.getText().toString();
        String passwordConfirm = etPasswordConfirm.getText().toString();


        if (TextUtils.isEmpty(account)) {
            ToastUtil.toastShort(this, "请输入用户名");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.toastShort(this, "请输入密码");
            return;
        }

        if (TextUtils.isEmpty(passwordConfirm)) {
            ToastUtil.toastShort(this, "请输入确认密码");
            return;
        }

        if (!cbAgree.isChecked()) {
            ToastUtil.toastShort(this, "请同意用户协议");
            return;
        }

        SharedPreferences spf = getSharedPreferences(SPF_TAG, MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putString("account", account);
        editor.putString("password", password);
        editor.apply();

        //回跳并带参数至登录页面
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("account", account);
        bundle.putString("password", password);
        intent.putExtras(bundle);

        setResult(RESULT_CODE_REGISTER, intent);
        ToastUtil.toastShort(this, "注册成功！");
        this.finish();
    }

    public void fnReLogin(View view) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }
}